//
//  PhotosViewController.m
//  Community-for-college
//
//  Created by 卖女孩的小火柴 on 16/9/18.
//  Copyright © 2016年 彭鑫. All rights reserved.
//
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#import "PhotosViewController.h"
#import "NSObject+alertView.h"
@interface PhotosViewController ()
{
    AVCaptureDevice *device;
}
@property(nonatomic, strong) AVCaptureSession *avSession;

@end

@implementation PhotosViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _bigimageView = [[UIImageView alloc]initWithFrame:(CGRectMake(0, 0, WIDTH, HEIGHT))];
    _bigimageView.image = [UIImage imageNamed:@"artWorkImage.png"];
    _bigimageView.alpha = 0.23;
    [self.view addSubview:_bigimageView];
    
    _imageView = [[UIImageView alloc]initWithFrame:(CGRectMake((self.view.frame.size.width - 200) /2, 100, 200, 200))];
    _imageView.image = [UIImage imageNamed:@"artWorkImage.png"];
    [self.view insertSubview:_imageView aboveSubview:_bigimageView];
    
    NSLog(@"打开系统图片库");
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;//是否可以对原图进行编辑
        
        //打开相册选择照片
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
    else{
        
        [NSObject alterString:@"图片库不可用"];
    }

    //启动照相机按钮
    UIButton *camera_Button = [[UIButton alloc]initWithFrame:(CGRectMake((WIDTH - 100) / 2, CGRectGetMaxY(_imageView.frame) + 30, 100, 30))];
    [camera_Button addTarget:self action:@selector(cameraButton) forControlEvents:(UIControlEventTouchUpInside)];
    [camera_Button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [camera_Button setTitle:@"图片库" forState:(UIControlStateNormal)];
    [self.view addSubview:camera_Button];
    
}
- (void)cameraButton
{
    NSLog(@"打开系统图片库");
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;//是否可以对原图进行编辑
        
        //打开相册选择照片
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
    else{
        
        [NSObject alterString:@"图片库不可用"];
    }

}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//编辑后的图片
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//把图片存到图片库
        self.imageView.image = image;
        self.bigimageView.image = image;
    }else{
        self.imageView.image = image;
        self.bigimageView.image = image;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
